package com.egao.common.publicly;

import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.cms.entity.Question;
import com.egao.common.publicly.service.PubliclyQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 常见问题
 * @author pyx
 */
@Api(tags = "常见问题")
@RestController
@RequestMapping("${api}")
public class PubliclyQuestionController {
    @Autowired
    private PubliclyQuestionService publiclyQuestionService;

    @ApiOperation("列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型(1教师 2学生 3单位)", required = false, dataType = "String", paramType = "query"),
    })
    @GetMapping("/publicly/question/list")
    public PageResult<Question> list(HttpServletRequest request){
        PageParam<Question> pageParam = new PageParam<>(request);
        return new PageResult<>(publiclyQuestionService.listPage(pageParam),pageParam.getTotal());
    }

    @ApiOperation("详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "常见问题id", required = true, dataType = "int", paramType = "query"),
    })
    @GetMapping("/publicly/question/get")
    public JsonResult get(Integer id){
        if(id == null){
            return JsonResult.error("没有获取到id");
        }

        Question question = publiclyQuestionService.getId(id);
        if(question == null){
            return JsonResult.ok("常见问题不存在");
        }

        return JsonResult.ok().setData(question);
    }
}
